define([
    'jquery',
    'underscore',
    'backbone',
    'text!module/article/templates/rpt_category.html',
    'text!module/article/templates/rpt_stock.html',
    'text!module/article/templates/rpt_valued.html'

], function($, _, Backbone, rpt_category, rpt_stock, rpt_valued) {

    var ReportArticleController = Backbone.View.extend({
        el: $("#page"),
        initialize: function() {
        },
        events: {
            "click #article_category_generate": "article_category_generate",
            "click #article_category_pdf": "article_category_pdf",
            "click #article_category_excel": "article_category_excel",
            "click #article_valued_generate": "article_valued_generate",
            "click #article_valued_pdf": "article_valued_pdf",
            "click #article_valued_excel": "article_valued_excel",
            "click #article_stock_generate": "article_stock_generate",
            "click #article_stock_pdf": "article_stock_pdf",
            "click #article_stock_excel": "article_stock_excel"
        },
        render: function() {

        },
        article_category: function() {
            this.$el.html(rpt_category);
            $.get('server/api/category/find', function(data) {
                $.each(data, function(i, item) {
                    var option = document.createElement('option');
                    $(option).val(item.id);
                    $(option).html(item.name);
                    $("#category_id").append(option);
                });
            }, "json");
        },
        article_category_generate: function() {
            $.get('server/api/art_rep/cat/' + $("#category_id option:selected").val(), function(data) {
                $.each(data, function(i, item) {
                    $("#rst_table").append("<tr><td>" + item.name + "</td><td>" + item.model + "</td></tr>");
                });
            }, "json");
        },
        article_category_pdf: function() {
            relocate("server/reports/article_reports.php", {
                report: "article_category_pdf",
                category_id: $("#category_id option:selected").val()
            });
        },
        article_category_excel: function() {
            relocate("server/reports/article_reports.php", {
                report: "article_category_excel",
                category_id: $("#category_id option:selected").val()
            });
        },
        article_valued: function() {
            this.$el.html(rpt_valued);
        },
        article_valued_generate: function() {
            $.get('server/api/art_rep/valued', function(data) {
                $.each(data, function(i, item) {
                    $("#rst_table").append(
                            "<tr>" +
                            "<td>" + item.name + "</td>" +
                            "<td>" + item.model + "</td>" +
                            "<td>" + item.stock + "</td>" +
                            "<td>" + item.price_purcharse + "</td>" +
                            "<td>" + item.price_sale + "</td>" +
                            "<td>" + CurrencyFormatted(parseFloat(item.stock) * parseFloat(item.price_purcharse)) + "</td>" +
                            "<td>" + CurrencyFormatted(parseFloat(item.stock) * parseFloat(item.price_sale)) + "</td>" +
                            "</tr>");
                });
            }, "json");
        },
        article_valued_pdf: function() {
            relocate("server/reports/article_reports.php", {
                report: "article_valued_pdf"
            });
        },
        article_valued_excel: function() {
            relocate("server/reports/article_reports.php", {
                report: "article_valued_excel"
            });
        },
        article_stock: function() {
            this.$el.html(rpt_stock);
        },
        article_stock_generate: function() {
            $.get('server/api/art_rep/valued', function(data) {
                $.each(data, function(i, item) {
                    $("#rst_table").append(
                            "<tr>" +
                            "<td>" + item.name + "</td>" +
                            "<td>" + item.model + "</td>" +
                            "<td>" + item.stock + "</td>" +
                            "<td>" + item.stock_minimun + "</td>" +
                            "</tr>");
                });
            }, "json");
        },
        article_stock_pdf: function() {
            relocate("server/reports/article_reports.php", {
                report: "article_stock_pdf"
            });
        },
        article_stock_excel: function() {
            relocate("server/reports/article_reports.php", {
                report: "article_stock_excel"
            });
        }
    });
    return ReportArticleController;
});

function relocate(page, params) {
    var body = document.body;
    form = document.createElement('form');
    form.method = 'POST';
    form.action = page;
    form.name = 'jsform';
    for (index in params)
    {
        var input = document.createElement('input');
        input.type = 'hidden';
        input.name = index;
        input.id = index;
        input.value = params[index];
        form.appendChild(input);
    }
    body.appendChild(form);
    form.submit();
}
function CurrencyFormatted(amount) {
    var i = parseFloat(amount);
    if (isNaN(i)) {
        i = 0.00;
    }
    var minus = '';
    if (i < 0) {
        minus = '-';
    }
    i = Math.abs(i);
    i = parseInt((i + .005) * 100);
    i = i / 100;
    s = new String(i);
    if (s.indexOf('.') < 0) {
        s += '.00';
    }
    if (s.indexOf('.') == (s.length - 2)) {
        s += '0';
    }
    s = minus + s;
    return s;
}
